package org.example.view;

import java.awt.*;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

import org.example.StudentApplication;
import org.example.dao.*;
import org.example.pojo.*;
import org.example.utils.ALiYunOssUtils;
import org.example.utils.BigModelNew;
import org.example.utils.EverydayUttils;

import static java.lang.System.in;

public class MeneView {
    public void showMainMenu(Scanner scanner, int id, String userName) throws Exception {
        while (true) {
            System.out.println("************************欢迎" + userName + "来到社区论坛*******************************");
            Announcement a = new AnnouncementsDao().getAnnouncement(1);
            System.out.println("\n\t\t\t\t\t\t\t公告:" + a.getTitle() + "\n\t\t\t\t\t\t\t\t\t" + a.getContent());
            System.out.println("\t\t\t\t\t\t\t\t\t\t" + new EverydayUttils().getEveryday());

            new MeneWindow().showMainMenu();
            System.out.println("请输入你要进行的操作：");
            String nextLine = scanner.nextLine();
            switch (nextLine) {
                case "1":  //查看文章
                {
                    int publish_index = 0;
                    // 分页 展示文章
                    publish_index = ViewArticle(id, publish_index);
                    List<Integer> ls = new ArrayList<>();
                    if(publish_index==0){
                        break;
                    }
                    ls = new FunctionsDao().likePublish(id, publish_index);
                    if (id != 0) {
                        functions(id, publish_index, userName, ls, -1);
                    } else {
                        functions(id, publish_index, userName, null, -1);
                    }
                }
                break;
                case "2":  // 发布文章
                    publish(scanner, id, userName);
                    break;
                case "3":  //搜索文章
                    new QueryArticleView().queryArticle(id);
                    break;
                //问道
                case "4": {
                    try {
                        new BigModelNew("1", true).BigMedel();
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                    break;
                }
                case "5": {
                    if (id != 0) {
                        new QQServer();
                    } else {
                        System.out.println("用户未登录,请先返回登录!");
                    }
                    break;
                }
                //我的主页
                case "6": {
                    if (id != 0) {
                        new CenterView().Mains(id);
                    } else {
                        System.out.println("用户未登录,请先返回登录!");
                    }
                    break;
                }
                //退出
                case "0": {
                    new ManageView().showLoadingAnimation();
                    try {
                        User user = new UserView().welcome();
                        if (user == null) {
                            System.out.println("欢迎下次使用！！！");
                            return;
                        }
                        new MeneView().showMainMenu(new java.util.Scanner(System.in), user.getId(), user.getUsername());
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                    return;
                }
                default:
                    System.out.println("输入错误,请重新输入");
                    break;
            }
        }
    }

    // 展示文章
    public int ViewArticle(int id, int publish_index) throws Exception {
        PublishDao publish = new PublishDao();
        List<Publish> listPublish = null;
        try {
            listPublish = publish.listPublish();
            // 分页 + 参看文章
            publish_index = new PagingView().showPaging(1, 8, listPublish.size(), 1, "", 0, "", "", 0);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        //退出
        if (publish_index == 0) {
            return 0;
        } else {
            return publish_index;
        }
    }

    //功能区
    public void fuctions(int publish_index) {
        // 点赞， 收藏， 转发
        List<Integer> ls = null;
        try {
            ls = new FunctionsDao().counts(publish_index);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        System.out.println("\n\n\t点赞: " + ls.get(0) + "\t收藏：" + ls.get(1) + "\t转发: " + ls.get(2));

        //评论
        System.out.println("\n\n评论区：\n");
        //展示评论
        List<Comment> comment = null;
        try {
            comment = new CommentDao().getComment(publish_index);
            for (Comment i : comment) {
                System.out.println("\t" + "序号：" + i.getId() + "   " + i.getUsername() + " : " + i.getContent() + "\t\t" + i.getCreateTime());
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    //点赞
    public void like(int id, int publish_index, List<Integer> ls) throws Exception {
        if (publish_index == 0) {
            return;
        }
        if (id == 0) {
            System.out.println("未登录，请先登录!");
            new StudentApplication();
            return;
        }
        // ==0: 没有数据->点赞
        boolean flag = ls.get(0) == 0;
        boolean b = new FunctionsDao().userLike(id, publish_index, flag);
        if (b) {
            System.out.println("点赞成功!");
        } else {
            System.out.println("取消点赞成功!");
        }
        new PagingView().showPublish(publish_index);
    }

    //收藏
    public void collect(int id, int publish_index, List<Integer> ls) throws Exception {
        if (publish_index == 0) {
            return;
        }
        if (id == 0) {
            System.out.println("未登录，请先登录!");
            new StudentApplication();
            return;
        }
        boolean flag = ls.get(1) == 0;
        boolean b = new FunctionsDao().userCollect(id, publish_index, flag);
        if (b) {
            System.out.println("收藏成功!");
        } else {
            System.out.println("取消收藏成功!");
        }
        new PagingView().showPublish(publish_index);
    }

    //转发
    public void retransmission(int id, int publish_index) throws Exception {
        if (publish_index == 0) {
            return;
        }
        List<String> list = new PublishDao().showIndexView(publish_index);
        //通过默认浏览器，去转发
        Desktop desktop = Desktop.getDesktop();
        if (Desktop.isDesktopSupported() && desktop.isSupported(Desktop.Action.BROWSE)) {
            try {
                URI uri = new URI(list.get(0));
                desktop.browse(uri);
            } catch (URISyntaxException e) {
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        //数据库 转发数加1；
        boolean b = new FunctionsDao().userRetransmission(id, publish_index);
        if (b) {
            System.out.println("转发成功!");
        } else {
            System.out.println("转发失败!");
        }
        new PagingView().showPublish(publish_index);
    }

    //评论区
    public void comment(int id, int publish_index, String userName) throws Exception {
        if (publish_index == 0) {
            return;
        }
        if (id == 0) {
            System.out.println("未登录，请先登录!");
            new StudentApplication();
            return;
        }
        System.out.println("请输入评论内容：");
        Scanner scanner = new Scanner(in);
        String content = scanner.nextLine();
        try {
            if (new CommentDao().insertComment(id, publish_index, userName, content)) {
                System.out.println("评论成功!");
                new PagingView().showPublish(publish_index);
            } else {
                System.out.println("评论失败!");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    //标签 转
    public String tag(int tags) throws Exception {
        String tagc = null;
        if (tags == 1) {
            tagc = "java";
        }
        if (tags == 2) {
            tagc = "大数据";
        }
        if (tags == 3) {
            tagc = "算法";
        }
        if (tags == 0) {
            tagc = "其他";
        }
        return tagc;
    }

    //发布文章
    public void publish(Scanner scanner, int id, String userName) throws Exception {
        if (id == 0) {
            System.out.println("未登录，请先登录!");
            new StudentApplication();
            return;
        }
        StringBuilder stringBuilder = new StringBuilder();
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
//                System.out.println(formatter.format(date));
        System.out.println("请输入文章标题：");
        String title = scanner.nextLine();
        System.out.println("请输入文章摘要：");
        String outline = scanner.nextLine();
        System.out.println("请输入文章标签：");
        System.out.println("\t1. java  \t2. 大数据");
        System.out.println("\t3. 算法   \t0.其他");
        int tags = scanner.nextInt();
        scanner.nextLine();
        System.out.println("请输入文章内容：");
        String content = scanner.nextLine();

        stringBuilder.append("\t\t\t\t" + title).append("\n\t" + userName).append("\t标签：" + tag(tags)).append("\t创建于：" + formatter.format(date))
                .append("\t更新于：" + formatter.format(date)).append("\n\n\n\t" + outline).append(content).append("\n\n\t" + content);
//                System.out.println(stringBuilder.toString());
//                    String url = new ALiYunOssUtils().uploadFile(2, userName, stringBuilder.toString());
        List<String> list = new ALiYunOssUtils().uploadFile(id, userName, stringBuilder.toString());
        String url = list.get(0);
        String objectName = list.get(1);
//                    System.out.println(url);
//                    System.out.println(objectName);
        try {
            boolean publish = new PublishDao().createPublish(title, outline, url, objectName, id, tags);
            if (publish) {
                int id1 = new PublishDao().getId(url);
                new FunctionsDao().insertFunctions(id, id1);
                System.out.println("发布成功!");
            }
        } catch (Exception e) {
            System.err.println("发布失败");
            throw new RuntimeException(e);
        }

    }

    // 综合 功能区
    public void functions(int id, int publish_index, String userName, List<Integer> ls, int id2) throws Exception {

        fuctions(publish_index);
        while (publish_index != 0) {
            ls = new FunctionsDao().likePublish(id, publish_index);
            if (!ls.isEmpty()) {
                if (ls.get(0) != 0) {
                    if (ls.get(1) != 0) {
                        if (new FollowDao().isFollow(id, new PublishDao().getUserIdByPublishId(publish_index))) {  // 关注作者 , 通过 用户id 去获取 文章作者
                            System.out.println("\n\n请输入操作数: 0: 返回  1: 添加评论  2: (已点赞)取消点赞  3：(已经收藏)取消收藏  4：转发  5：删除评论  6: (已关注)取消关注");
                        } else {
                            System.out.println("\n\n请输入操作数: 0: 返回  1: 添加评论  2: (已点赞)取消点赞  3：(已收藏)取消收藏  4：转发  5：删除评论   6: 关注作者");
                        }
                    } else {
                        if (new FollowDao().isFollow(id, new PublishDao().getUserIdByPublishId(publish_index))) {  // 关注作者 , 通过 用户id 去获取 文章作者
                            System.out.println("\n\n请输入操作数: 0: 返回  1: 添加评论  2: (已点赞)取消点赞  3：收藏  4：转发  5：删除评论  6: (已关注)取消关注");
                        } else {
                            System.out.println("\n\n请输入操作数: 0: 返回  1: 添加评论  2: (已点赞)取消点赞  3：收藏  4：转发  5：删除评论   6: 关注作者");
                        }
                    }
                } else {
                    if (ls.get(1) != 0) {
                        if (new FollowDao().isFollow(id, new PublishDao().getUserIdByPublishId(publish_index))) {  // 关注作者 , 通过 用户id 去获取 文章作者
                            System.out.println("\n\n请输入操作数: 0: 返回  1: 添加评论  2: 点赞  3：(已经收藏)取消收藏  4：转发  5：删除评论  6: (已关注)取消关注");
                        } else {
                            System.out.println("\n\n请输入操作数: 0: 返回  1: 添加评论  2: 点赞  3：(已收藏)取消收藏  4：转发  5：删除评论   6: 关注作者");
                        }
                    } else {
                        if (new FollowDao().isFollow(id, new PublishDao().getUserIdByPublishId(publish_index))) {  // 关注作者 , 通过 用户id 去获取 文章作者
                            System.out.println("\n\n请输入操作数: 0: 返回  1: 添加评论  2: 点赞  3：收藏  4：转发  5：删除评论  6: (已关注)取消关注");
                        } else {
                            System.out.println("\n\n请输入操作数: 0: 返回  1: 添加评论  2: 点赞  3：收藏  4：转发  5：删除评论   6: 关注作者");
                        }
                    }
                }
            } else {
                if (new FollowDao().isFollow(id, new PublishDao().getUserIdByPublishId(publish_index))) {  // 关注作者 , 通过 用户id 去获取 文章作者
                    System.out.println("\n\n请输入操作数: 0: 返回  1: 添加评论  2: 点赞  3：收藏  4：转发  5：删除评论  6: (已关注)取消关注");
                } else {
                    System.out.println("\n\n请输入操作数: 0: 返回  1: 添加评论  2: 点赞  3：收藏  4：转发  5：删除评论   6: 关注作者");
                }
            }
            if (id == id2) {  // 删除文章
                System.out.println("\t\t\t7: 删除文章");
            }

            switch (new Scanner(in).nextLine()) {
                //返回
                case "0":
//                    publish_index = ViewArticle(id, publish_index);
                    return;
                //添加评论
                case "1": {
                    if (id != 0) {
                        comment(id, publish_index, userName);

                        fuctions(publish_index);
                    } else {
                        System.out.println("用户未登录，请先登录!");
                    }
                    break;
                }
                //点赞  1. 判断是否点赞过 2. 判断是否是本人 3. 点赞 4. 取消点赞
                case "2": {
                    if (id != 0) {
                        like(id, publish_index, ls);
                        fuctions(publish_index);
                    } else {
                        System.out.println("用户未登录，请先登录!");
                    }
                    break;
                }
                //收藏
                case "3": {
                    if (id != 0) {
                        collect(id, publish_index, ls);
                        fuctions(publish_index);
                    } else {
                        System.out.println("用户未登录，请先登录!");
                    }
                    break;
                }
                //转发,一个人可以转发多次
                case "4": {
                    if (id!=0) {
                        retransmission(id, publish_index);
                        fuctions(publish_index);
                    } else {
                        System.out.println("用户未登录，请先登录!");
                    }
                    break;
                }
                case "7": {  // 删除文章
                    if(id==0){
                        System.out.println("用户未登录，请先登录!");
                        break;
                    }
                    System.out.println("是否确认删除文章，删除后评论也将删除,请选择：y/n");
                    if (new Scanner(System.in).nextLine().equals("y")) {
                        new PublishDao().deletePublishById(publish_index);
                        System.out.println("删除文章成功");
                        break;
                    }
                    new PagingView().showPublish(publish_index);
                    fuctions(publish_index);
                    break;
                }
                case "5": {  // 删除评论
                    if(id==0){
                        System.out.println("用户未登录，请先登录!");
                        break;
                    }
                    System.out.println("请选择要删除的评论序号： ");
                    int index = new Scanner(System.in).nextInt();
                    if (userName.equals(new CommentDao().getUsername(index))) {
                        new CommentDao().deleteComment(index);
                        System.out.println("删除成功！");
                        new PagingView().showPublish(publish_index);
                        fuctions(publish_index);
                    } else {
                        System.out.println("删除失败，只能删除自己发布的评论");
                    }
                    break;
                }
                case "6": {  // 关注作者

                    if (id != 0) {
                        if (id != new PublishDao().getUserIdByPublishId(publish_index)) {  // 如果 为本人 ，关注失败
                            if (new FollowDao().isFollow(id, new PublishDao().getUserIdByPublishId(publish_index))) {
                                new FollowDao().deleteFollow(id, new PublishDao().getUserIdByPublishId(publish_index));
                                System.out.println("取消关注成功");
                            } else {
                                new FollowDao().addFollow(id, new PublishDao().getUserIdByPublishId(publish_index));
                                System.out.println("关注成功");
                            }
                        } else {
                            System.out.println("不能关注自己");
                        }
                        new PagingView().showPublish(publish_index);
                        fuctions(publish_index);
                    } else {
                        System.out.println("用户未登录，请先登录!");
                    }
                    break;
                }
                default:
                    System.out.println("输入错误，请重新输入!");
                    break;
            }
        }
    }

}
